package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    DtsRoleMapper dtsRoleMapper;
    @Override
    public Set<String> findByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DtsRole::getId,roleIds);
        List<DtsRole> dtsRoles = dtsRoleMapper.selectList(queryWrapper);
        Set<String> roleStrs = dtsRoles.stream().map(Role-> {
            return Role.getName();
        }).collect(Collectors.toSet());
        return roleStrs;
    }

    @Override
    public List<DtsRole> findAll() {


        return dtsRoleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> findByPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole> iPage = new Page<>(page,limit);
        QueryWrapper<DtsRole> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(rolename)){
            qw.like("name",rolename);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        dtsRoleMapper.selectPage(iPage,qw);


        return iPage;
    }



    @Override
    public DtsRole add(DtsRole dtsRole) {
        QueryWrapper<DtsRole> qw = new QueryWrapper<>();
        qw.eq("name",dtsRole.getName());
        DtsRole role = dtsRoleMapper.selectOne(qw);


        if(role==null){
            dtsRole.setAddTime(new Date());
            dtsRole.setUpdateTime(new Date());
            dtsRole.setDeleted(false);
            dtsRoleMapper.insert(dtsRole);
            return dtsRole;
        }else {
            return null;
        }



    }

    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole role = dtsRoleMapper.selectById(roleId);
        if("超级管理员".equalsIgnoreCase(role.getName())){
            return true;
        }
        return false;
    }


    @Override
    public DtsRole update(DtsRole dtsRole) {
        dtsRole.setUpdateTime(new Date());

        dtsRoleMapper.updateById(dtsRole);
        return dtsRole;
    }

    @Autowired
    DtsAdminMapper dtsAdminMapper;
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Override
    public boolean delete(DtsRole role) {
        //如果该角色被管理员使用，则不能删除
        LambdaQueryWrapper<DtsAdmin> qw1 = new LambdaQueryWrapper<>();
        qw1.select(DtsAdmin::getRoleIds);
        List<DtsAdmin> adminList = dtsAdminMapper.selectList(qw1);
        for (DtsAdmin admin : adminList) {
            Integer[] roleIds = admin.getRoleIds();
            for (Integer roleId : roleIds) {
                if(roleId.equals(role.getId())) {
                    return false;
                }
            }
        }

        LambdaQueryWrapper<DtsPermission> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsPermission::getRoleId, role.getId());
        Integer count = permissionMapper.selectCount(qw2);
        if(count >= 1) {
            return  false;
        }

        //如果没有被管理员和权限关联，则删除
        dtsRoleMapper.deleteById(role.getId());
        return true;
    }


}
